package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.LoginAnnotation;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.cache.service.IStrategyDataRedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategieController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;


    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyDataRedisService strategyDataRedisService;

    @GetMapping("/detail")
    private Object detail(String id){

        // 进入到控制器一次,表示阅读数+1,此操作是Redis操作
        strategyDataRedisService.readNum(id);

        Strategy strategy = strategyService.get(id);
        return JsonResult.success(strategy);
    }

    @GetMapping("/themes")
    private Object themes(){
        List<StrategyTheme> list = strategyThemeService.list();
        return JsonResult.success(list);
    }



    @GetMapping("/query")
    private Object query(StrategyQuery qo){
        Page<Strategy> query = strategyService.query(qo);
        return JsonResult.success(query);
    }


    @LoginAnnotation
    @PostMapping("/addComment")
    public Object addComment(StrategyComment strategyComment,@UserParam UserInfo user){
        BeanUtils.copyProperties(user,strategyComment);
        strategyComment.setUserId(user.getId());
        strategyCommentService.addComment(strategyComment);

        // 评论成功之后,对应的该攻略的评论数需要进行+1操作,此操作是Redis操作
        strategyDataRedisService.replynum(strategyComment.getStrategyId());

        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        Page<StrategyComment> list = strategyCommentService.query(qo);
        return JsonResult.success(list);
    }


    @LoginAnnotation
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid,@UserParam UserInfo user){
        strategyCommentService.commentThumb(cid,user.getId());
        return JsonResult.success();
    }



    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        StrategyStatisVO vo = strategyDataRedisService.get(sid);
        return JsonResult.success(vo);
    }


    @LoginAnnotation
    @PostMapping("/favor")
    public Object favor(String sid,@UserParam UserInfo user){
        boolean favor = strategyDataRedisService.favor(sid, user.getId());
        return JsonResult.success(favor);
    }


    @LoginAnnotation
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo user){
        boolean thumbup = strategyDataRedisService.strategyThumbup(sid, user.getId());
        return JsonResult.success(thumbup);
    }



































//    @GetMapping("/info")
//    public Object info(@UserParam UserInfo userInfo){
//        return JsonResult.success(userInfo);
//    }
//
//    @GetMapping("/info2")
//    public Object info2(UserInfo userInfo){
//        return JsonResult.success(userInfo);
//    }


}
